package com.ncst.backend.controller;

import com.ncst.backend.common.Result;
import com.ncst.backend.domain.MedicalRecord;
import com.ncst.backend.domain.vo.MedicalRecordVO;
import com.ncst.backend.service.MedicalRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 病历控制器
 */
@Api(tags = "病历管理")
@Slf4j
@RestController
@RequestMapping("/medical-records")
public class MedicalRecordController {

    @Autowired
    private MedicalRecordService medicalRecordService;

    @ApiOperation("根据ID查询病历记录")
    @GetMapping("/id/{id}")
    public Result<MedicalRecord> getById(@ApiParam("病历ID") @PathVariable Integer id) {
        MedicalRecord medicalRecord = medicalRecordService.getById(id);
        return medicalRecord != null ? Result.success(medicalRecord) : Result.notFound("病历记录不存在");
    }

    @ApiOperation("查询所有病历记录")
    @GetMapping
    public Result<List<MedicalRecord>> getAll() {
        return Result.success(medicalRecordService.getAll());
    }

    @ApiOperation("根据条件查询病历记录")
    @PostMapping("/search")
    public Result<List<MedicalRecord>> getByCondition(@ApiParam("查询条件") @RequestBody MedicalRecord medicalRecord) {
        return Result.success(medicalRecordService.getByCondition(medicalRecord));
    }

    @ApiOperation("分页查询病历记录")
    @GetMapping("/page")
    public Result<List<MedicalRecord>> getByPage(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(medicalRecordService.getByPage(pageNum, pageSize));
    }

    @ApiOperation("统计病历记录总数")
    @GetMapping("/count")
    public Result<Long> countAll() {
        return Result.success(medicalRecordService.countAll());
    }

    @ApiOperation("根据条件统计病历数量")
    @PostMapping("/count")
    public Result<Long> countByCondition(@ApiParam("查询条件") @RequestBody MedicalRecord medicalRecord) {
        return Result.success(medicalRecordService.countByCondition(medicalRecord));
    }

    @ApiOperation("新增病历记录（病案首页）")
    @PostMapping
    public Result<Void> save(@ApiParam("病历信息") @RequestBody MedicalRecordVO medicalRecordVo) {
        log.info("新增病历记录: {}", medicalRecordVo.toString());
        return medicalRecordService.save(medicalRecordVo) ? Result.success("病案首页新增成功") : Result.error("病案首页新增失败");
    }

    @ApiOperation("批量新增病历记录")
    @PostMapping("/batch")
    public Result<Void> saveBatch(@ApiParam("病历列表") @RequestBody List<MedicalRecord> list) {
        return medicalRecordService.saveBatch(list) ? Result.success("批量新增病历成功") : Result.error("批量新增病历失败");
    }

    @ApiOperation("根据ID更新病历记录")
    @PutMapping("/{id}")
    public Result<Void> updateById(@ApiParam("病历ID") @PathVariable Integer id, @ApiParam("病历信息") @RequestBody MedicalRecord medicalRecord) {
        medicalRecord.setId(id);
        return medicalRecordService.updateById(medicalRecord) ? Result.success("更新病历成功") : Result.error("更新病历失败");
    }

    @ApiOperation("根据ID删除病历记录")
    @DeleteMapping("/{id}")
    public Result<Void> removeById(@ApiParam("病历ID") @PathVariable Integer id) {
        return medicalRecordService.removeById(id) ? Result.success("删除病历成功") : Result.error("删除病历失败");
    }

    @ApiOperation("批量删除病历记录")
    @DeleteMapping("/batch")
    public Result<Void> removeByIds(@ApiParam("病历ID列表") @RequestBody List<Integer> ids) {
        return medicalRecordService.removeByIds(ids) ? Result.success("批量删除病历成功") : Result.error("批量删除病历失败");
    }

    @ApiOperation("根据挂号ID查询病历记录")
    @GetMapping("/register/{registerId}")
    public Result<MedicalRecord> getByRegisterId(@ApiParam("挂号ID") @PathVariable Integer registerId) {
        MedicalRecord medicalRecord = medicalRecordService.getByRegisterId(registerId);
        return medicalRecord != null ? Result.success(medicalRecord) : Result.notFound("该挂号记录对应的病历不存在");
    }

    @ApiOperation("门诊确诊")
    @RequestMapping("outpatientDiagnosis")
    public Result<Void> outpatientDiagnosis(
            @RequestParam("register_id") String register_id,
            @RequestParam("diagnosis") String diagnosis,
            @RequestParam("cure") String cure) {
        boolean b = medicalRecordService.menZhenQueZhen(register_id, diagnosis, cure);
        return b ? Result.success("门诊确诊成功") : Result.error("门诊确诊失败");
    }
}
